package com.valuegroup.smarttodo.ui;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.plugins.imgsel.utils.StatusBarCompat;
import com.valuegroup.smarttodo.ui.cognize.CognizeFragment;
import com.valuegroup.smarttodo.ui.knowledge.KnowledgeFragment;
import com.valuegroup.smarttodo.ui.programme.ProgrammeFragment;
import com.valuegroup.smarttodo.ui.todo.TodoFragment;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import butterknife.ButterKnife;

import static com.valuegroup.smarttodo.util.PermisionUtils.verifyStoragePermissions;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private TodoFragment todoFragment;
    private ProgrammeFragment programmeFragment;
    private KnowledgeFragment knowledgeFragment;
    private CognizeFragment cognizeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        verifyStoragePermissions(this);

        // 内容栏
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // 默认选中为待办栏目
        todoFragment = TodoFragment.newInstance();
        transaction.replace(R.id.main_fragment_container, todoFragment);
        transaction.commit();

        // 首页底部导航
        initBottomNavigationBar();
    }


    /**
     * 初始化底部导航模块
     */
    private void initBottomNavigationBar() {

        BottomNavigationBar bottomNavigationBar = findViewById(R.id.main_navigation_bar);

        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        // 固定底部状态栏
        bottomNavigationBar.setAutoHideEnabled(false);

        // 标记（可以用于显示未读数量等）
        //TextBadgeItem badgeItem = new TextBadgeItem().setBorderWidth(1).setBackgroundColorResource(R.color.colorAccent).setText("2").setHideOnSelect(true);

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_part1_gray3, R.string.todo).setActiveColorResource(R.color.colorPrimary))//.setBadgeItem(badgeItem)
                .addItem(new BottomNavigationItem(R.drawable.ic_part2_gray3, R.string.programme).setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.ic_part3_gray3, R.string.knowledge).setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.ic_part4_gray3, R.string.cognize).setActiveColorResource(R.color.colorPrimary))
                .setFirstSelectedPosition(0)
                .initialise();

        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (todoFragment == null) {
                    todoFragment = TodoFragment.newInstance();
                }
                transaction.replace(R.id.main_fragment_container, todoFragment);
                break;
            case 1:
                if (programmeFragment == null) {
                    programmeFragment = ProgrammeFragment.newInstance();
                }
                transaction.replace(R.id.main_fragment_container, programmeFragment);
                break;
            case 2:
                if (knowledgeFragment == null) {
                    knowledgeFragment = KnowledgeFragment.newInstance();
                }
                transaction.replace(R.id.main_fragment_container, knowledgeFragment);
                break;
            case 3:
                if (cognizeFragment == null) {
                    cognizeFragment = CognizeFragment.newInstance();
                }
                transaction.replace(R.id.main_fragment_container, cognizeFragment);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

}
